<template>
	<view class="content">
		<view class="" v-for="(item,index) in list" :key="index">
			<view class="top">
				<image class="top-image" :src="img+item.THUMB" mode=""  ></image>
				<view class="top-text" v-if="type==2">
					<view class="">{{item.TITLE}}</view>
					<view class="">{{item.SALEPRICE}}元</view>
				</view>
				<view class="top-text" v-else>
					<view class="">{{item.NAME}}</view>
					<view class="" v-if="item.MULTIPAY==2" >
						<text  class="price1" v-if="item.OPPOINT>0">积分:{{item.OPPOINT}}</text>
						<text  class="price1"  v-if="item.OPDEDU>0">抵扣券:{{item.OPDEDU}}</text>
						<text  class="price1"  v-if="item.OPCOUPON>0">优惠券:{{item.OPCOUPON}}</text>
					</view>
					<view class="" v-else >
						<text  class="price"  v-if="item.MUCHSELLERPRICE>0">{{item.MUCHSELLERPRICE}}</text>
						<text  class="price1"  v-if="item.MPPOINT>0">+积分:{{item.MPPOINT}}</text>
						<text  class="price1"  v-if="item.MPDEDU>0">+抵扣券:{{item.MPDEDU}}</text>
						<text  class="price1"  v-if="item.MPCOUPON>0">+优惠券:{{item.MPCOUPON}}</text>
					</view>
				</view>
			</view>
			<view class="" @click="abcd(index)">
				<my-issue  headPicShow="false" @submitt="submit" @submitShow="false" @imageSrc="imagess"  @scoreChange="Change"   @inputs="inputa" @flag="buttomtype"/>
			</view>
			
		</view>
		<view class="" >
			<button type="primary"  @click="submit" style="background-color: #E02E24;">提交</button>
		</view>
	</view>
</template>

<script>
  import myIssue from '@/components/myIssue.vue'
	export default {
		components:{
			myIssue
			},
		data() {
			return {
				score: '',
				text: '',
				flag: false,
				orderNum: '',
				imgs:[],
				list: [],
				img: '',
				type:'',
				indee:'',
				scorelist:[],
				textlist:[],
				flaglist:[],
				imgslist:[],
				ids:[]
			}
		},
		onLoad(options) {
			this.img = getApp().globalData.imageAddress;
			this.orderNum = options.ORDERNUM
			this.loadDate()
			uni.startPullDownRefresh()
			
		},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		
		methods: {
			
			async loadDate(){
				this.$http.post('/order/orderDetail',{
					SIGNATURE : uni.getStorageSync("SIGNATURE"),
					TOKEN: uni.getStorageSync("userInfo").TOKEN,
					ORDERNUM: this.orderNum
				}).then((res)=>{
					//判断返回内容
					if(res.data.STATE === "0" && res.statusCode === 200){
						var demo = res.data.OBJECT
						this.list.length = 0
						if(demo.TYPE == 1){
							this.type = 1
							this.list.push(demo)
						}else if(demo.TYPE == 2){
							this.type = 2
							this.list = demo.LIST
						}
						console.log(">>>>>>>>>>>>>>>>>>",this.list)
						this.ids = this.list.map(item=>{
							return item.ID
						})
					}else{
						uni.showToast({title:res.data.MSG, icon:"none"});
					}
				})
				.catch((err)=>{
				    console.log('request fail', err);
				})
			},
			
			Change(score){
				this.score = score;
			},
			abcd(index){
				console.log("****",index)
				this.indee = index
				var scores = {}
				scores.id = index
				scores.score = this.score
				let count1 = 0
				this.scorelist.forEach(item=>{
					if(index == item.id){
						item.score = this.score
						count1 ++
					}
				})
				if(count1 == 0){
					this.scorelist.push(scores)
				}
				
				var flags = {}
				flags.id = index
				flags.flag = this.flag
				let count3 = 0
				this.flaglist.forEach(item=>{
					if(index == item.id){
						item.flag = this.flag
						count3 ++
					}
				})
				if(count3 == 0){
					this.flaglist.push(flags)
				}
			},
			inputa(value){
				this.text = value;
				var texts = {}
				texts.id = this.indee
				texts.text = value
				let count = 0
				this.textlist.forEach(item=>{
					if(this.indee == item.id){
						item.text = this.text
						count ++
					}
				})
				if(count == 0){
					this.textlist.push(texts)
				}
				
			},
			buttomtype(flag){
				this.flag = flag;
				
			},
			imagess(imgs){
				this.imgs = imgs;
				console.log("6666",imgs)
				var img = {}
				img.id = this.indee
				img.img = imgs
				let count = 0
				this.imgslist.forEach(item=>{
					if(this.indee == item.id){
						item.img = imgs
						count ++
					}
				})
				if(count == 0){
					this.imgslist.push(img)
				}
				
				console.log("图片数组",this.imgslist)
			},
			upload(index,LUNBONUM){
				var imageSrc = [] 
				this.imgslist.forEach(item1=>{
					if(item1.id == index){
						item1.img.forEach(item=>{
							uni.uploadFile({
								url: getApp().globalData.http+'/user/fileUpload?SIGNATURE='+
								uni.getStorageSync("SIGNATURE")+"&LUNBONUM="+LUNBONUM,
								filePath: item,
								fileType: 'image',
								name: 'data',
								success: (res1) => {
									console.log('uploadImage success, res is:', res1)
								},
								fail: (err1) => {
									console.log('uploadImage fail', err1);
									uni.showModal({
										content: err1.errMsg,
										showCancel: false
									});
								}
							});
						})
					}
				})
				
				
					
			},
			// 提交
			submit(){
				uni.showLoading({
					title: '请稍后'
				})
				var score = []
				var text = []
				var flag = []
				let count1 = 0 
				this.scorelist.forEach(item=>{
					if(item.score == 0 ){
						count1 ++
					}else{
						score.push(item.score)
					}
				})
				if(count1 != 0 || this.scorelist.length!=this.list.length ){
					uni.showToast({title:"请填写商品评分", icon:"none"});
					return;
				}
				count1= 0
				this.textlist.forEach(item=>{
					if(item.text.length == 0){
						count1 ++
					}else{
						text.push(item.text)
					}
				})
				if(count1 != 0 || this.textlist.length!=this.list.length ){
					uni.showToast({title:"请填写商品评分", icon:"none"});
					return;
				}
				this.flaglist.forEach(item=>{
					var a = item.flag ? '1' : '0'
					flag.push(a)
				})
				for(let i=0;i<score.length ; i++){
					this.$http.post('/user/comment',{
						SIGNATURE : uni.getStorageSync("SIGNATURE"),
						TOKEN: uni.getStorageSync("userInfo").TOKEN,
						FLAG: flag[i] ,
						SCORE: score[i],
						COMMENT: text[i],
						SPECID: this.ids[i],
						ORDERNUM: this.orderNum
					}).then((res)=>{
						//判断返回内容
						if(res.data.STATE === "0" && res.statusCode === 200){
							this.upload(i,res.data.OBJECT)
						}else{
							uni.showToast({title:res.data.MSG, icon:"none"});
						}
					})
					.catch((err)=>{
					    console.log('request fail', err);
					})
				}
				setTimeout(function(){
					uni.showToast({title:"评论成功", icon:"none"});
					uni.hideLoading();
					uni.redirectTo({
						url: '/pages/order/order?state=3'
					})
				},1500)
				
			}
		}
	}
</script>

<style lang="scss">
	.content{
		background-color :  $page-color-base;
		.top{
			display: flex;
			padding-left: 40upx;
			padding-top: 30upx;
			
			
			.top-image{
				height: 150upx;
				width: 150upx;
			}
			.top-text{
				padding-left: 30upx;
				font-size: 30upx;
				// margin-bottom: 20upx;
			}
		}
	}
</style>
